package com.byp.java_sport.utils;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;

import java.nio.charset.StandardCharsets;

public class PasswordUtils {
    /**
     * 密码加密
     * @param slat
     * @param password
     * @return
     */
    public static String encrypt(String slat,String password) {
//        生成秘钥
        byte[] encoded = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue(), slat.getBytes()).getEncoded();

        return SecureUtil.aes(encoded).encryptBase64(password);
    }
    /**
     * 密码解密
     * @param slat
     * @param encryptPassword
     * @return
     */
    public static String decrypt(String slat,String encryptPassword) {
//        生成秘钥
        byte[] encoded = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue(), slat.getBytes()).getEncoded();

        return SecureUtil.aes(encoded).decryptStr(encryptPassword);
    }


    public static void main(String[] args) {
        String sl = RandomUtil.randomString(16);
        String aaa123456 = encrypt(sl, "aaa123456");
        String decrypt = decrypt(sl, aaa123456);
        System.out.println("aaa123456 = " + aaa123456);
        System.out.println(decrypt);


    }
}
